﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO;
namespace DAO
{
    public class HTTTDAO
    {
        public bool KtHTTT(string TenHTTT)
        {
            string sql = "SELECT COUNT(*) FROM HinhThucThanhToan WHERE TenHTTT = N'" + TenHTTT + "'";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                read.Read();
                int kq = read.GetInt32(0);
                conn.Close();
                if (kq == 0)
                    return true;
                return false;
            }
        }
        public bool ThemHTTT(string TenHTTT)
        {
            string sql = "INSERT INTO HinhThucThanhToan(TenHTTT, Deleted) VALUES(N'"+TenHTTT+"', 0)";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 0)
                    return true;
                return false;
            }
        }
        public List<HTTTDTO> LayHTTT()
        {
            string sql = "SELECT * FROM HinhThucThanhToan WHERE Deleted = 0";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                List<HTTTDTO> listHTTT = new List<HTTTDTO>();
                SqlDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    HTTTDTO httt = new HTTTDTO();
                    httt.MaHTTT = read.GetInt32(0);
                    httt.TenHTTT = read.GetString(1);
                    listHTTT.Add(httt);
                }
                conn.Close();
                return listHTTT;
            }
        }
        public HTTTDTO LayHTTTTheoMa(int MaHTTT)
        {
            string sql = "SELECT * FROM HinhThucThanhToan WHERE MaHTTT = "+MaHTTT;
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader read = cmd.ExecuteReader();
                HTTTDTO httt = new HTTTDTO();
                read.Read();
                httt.MaHTTT = read.GetInt32(0);
                httt.TenHTTT = read.GetString(1);
                return httt;
            }
        }
        public bool SuaHTTT(string TenHTTTMoi, string TenHTTTCu)
        {
            string sql = "UPDATE HinhThucThanhToan SET TenHTTT = N'"+TenHTTTMoi+"' WHERE TenHTTT = N'"+TenHTTTCu+"'";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 0)
                    return true;
                return false;
            }
        }
        public bool XoaHTTT(string TenHTTT)
        {
            string sql = "UPDATE HinhThucThanhToan SET Deleted=1 WHERE TenHTTT = N'"+TenHTTT+"'";
            using (SqlConnection conn = DataProvider.ConnectDB())
            {
                SqlCommand cmd = new SqlCommand(sql, conn);                
                int kq = cmd.ExecuteNonQuery();
                conn.Close();
                if (kq >= 0)
                    return true;
                return false;
            }
        }
    }
}
